-- StorageBox
-- Created by chengb Feb/3/2016
-- 储物箱格子

-- 初始化
function initStorageBox(self)
    -- 查看储物箱
    local pos = self.gridData:getPos();
    local function viewStorageBox()
        if DungeonInstanceM.isInInstance() then
            -- 如果是副本的中心格子，走副本奖励流程
            local targetGrid = DungeonInstanceM.getTargetGrid();
            if targetGrid and targetGrid:getPos() == pos then
                DungeonActionM.go("take_instance_bonus", pos);
            else
                DungeonActionM.go("do_building_bonus", pos);
            end
        else
            -- 不在副本中，走通用奖励流程
            DungeonActionM.go("do_building_bonus", pos);
        end

        EventMgr.fire(event.PLAYER_MOVE, pos);
    end

    local class = self.gridData.class;
    local buildingInfo = BuildingBonusM.queryBuildingView(class);

    if not buildingInfo then
        error("获取不到建筑查看配置，元素类型：" .. class);
        return;
    end

    local function onClicked(sender, eventType)
        if eventType == ccui.TouchEventType.began then
            if self.gridData.state == GRID_STATE_DISABLE then
                -- 已经失效，不处理
                return;
            end

            -- boss层中
            local dungeonId = DungeonM.getDungeonId();
            local layer     = DungeonM.currentLayer();
            if DungeonBossM.isBossLayer(dungeonId, layer) and not DungeonM.isAllMonsterDead() then
                alert(getLocStr("pick_up_instance_target_fail"));
                return;
            end

            -- 更新建筑访问
            self:updateBuildingVisit();

            -- 音效
            AudioM.playFx("ui_open1");

            if DungeonInstanceM.isInInstance() and
                not DungeonM.isAllMonsterDead() then
                -- 如果在副本中，需要击杀所有怪物方可拾取
                self:openBuildingView(nil, -20, buildingInfo["dialog2"], getLocStr("btn_text_leave"));
                return;
            end

            -- 打开建筑查看界面
            self:openBuildingView(viewStorageBox);
        end
    end

    -- 建筑图标
    local iconPath = getLevelItemIconPath(buildingInfo["icon"]);

    self.icon = ccui.ImageView:create(iconPath);
    self.icon:setTouchEnabled(true);
    self.icon:addTouchEventListener(onClicked);
    self.item:addChild(self.icon);
    self.iconPath = iconPath;
end
